package com.itheima.web.controller.message;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheiam.service.message.ProductService;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.domain.message.Product;
import com.itheima.domain.message.ProductExample;
import com.itheima.service.cargo.FactoryService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/message/product")
public class ProductController extends BaseController {

    @Reference
    private ProductService productService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list",name = "分页查询商品信息列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo<Product> pageInfo = productService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);
        return "message/product/product-list";
    }

    @RequestMapping(value = "/toAdd", name = "跳转到商品新增页面")
    public String toAdd() {
        FactoryExample example = new FactoryExample();
        FactoryExample.Criteria criteria = example.createCriteria();
        criteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(example);

        request.setAttribute("factoryList",factoryList);
        return "message/product/product-add";
    }

    @RequestMapping(value = "toUpdate", name = "跳转到商品修改页面")
    public String toUpdate(String id) {
        Product product = productService.findById(id);

        FactoryExample example = new FactoryExample();
        FactoryExample.Criteria criteria = example.createCriteria();
        criteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(example);

        request.setAttribute("factoryList",factoryList);
        request.setAttribute("product", product);
        return "message/product/product-update";
    }

    @RequestMapping(value = "edit", name = "提交修改或删除")
    public String edit(Product product) {
        if (StringUtils.isEmpty(product.getId())) {
            product.setCompanyId(companyId);
            product.setCompanyName(companyName);
            productService.save(product);
        } else {
            productService.update(product);
        }
        return "redirect:/message/product/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除商品")
    public String delete(String id) {
        productService.delete(id);
        return "redirect:/message/product/list.do";
    }
}
